package com.beaven.imclient.utils;

import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;

/**
 * created by beaven on 2017/5/5 13:43.
 * <p>
 */

public class ActivityUtil {
  public static void replaceFragmentToActivity(@NonNull FragmentManager manager,
      @NonNull Fragment fragment, int fragmentId) {
    replaceFragmentToActivity(manager, fragment, fragmentId, null);
  }

  public static void replaceFragmentToActivity(@NonNull FragmentManager manager,
      @NonNull Fragment fragment, int fragmentId, String tag) {
    Fragment fragment1 = manager.findFragmentByTag(tag);
    if (fragment1 != null) {
      return;
    }
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.replace(fragmentId, fragment, tag);
    transaction.commit();
  }

  public static AlertDialog showDialog(Context context, String title, String msg,
      DialogInterface.OnClickListener listener) {
    return new AlertDialog.Builder(context).setTitle(title)
        .setMessage(msg)
        .setPositiveButton("确定", listener)
        .setNegativeButton("取消", null)
        .create();
  }
}
